<?php

namespace app\store\service\wxapp;

use app\common\library\wechat\WxTplMsg;
use app\common\service\BaseService;

/**
 * 推送模板消息服务类-公众号
 * Class Message
 * @package app\store\service\wxapp
 */
class Message extends BaseService {
	// 分割符号
	const SEPARATOR = ',';

	/** @var array $stateSet 状态集 */
	private $stateSet = [];

	/** @var WxTplMsg $WxTplMsg 微信模板消息类 */
	private $WxTplMsg;

	/**
	 * 构造方法
	 * Message constructor.
	 * @throws \app\common\exception\BaseException
	 * @throws \think\exception\DbException
	 */
	public function __construct() {
		// 实例化：微信模板消息类

		$app_id = 'wx5e91f8054325bf08';
		$app_secret = 'fbe20685ae2307a9c09a520cdb902887';
		$this->WxTplMsg = new WxTplMsg($app_id, $app_secret);
	}

	/**
	 * @param $data
	 * @return bool
	 * @throws \app\common\exception\BaseException
	 * @throws \think\exception\DbException
	 */
	public function send($open_id, $data) {

		$this->sendTemplateMessage($open_id, $data);
		return true;
	}

	/**
	 * 发送模板消息
	 * @param $userId
	 * @param $data
	 * @return bool
	 * @throws \app\common\exception\BaseException
	 * @throws \think\exception\DbException
	 */
	private function sendTemplateMessage($open_id, $data) {

		//小程序appid
		$appid = 'wxee43c66f451d0b81';

		// 构建模板消息参数
		$miniprogram['appid'] = $appid;
		$miniprogram['pagepath'] = $data['page'];
		$params = [
			'touser' => $open_id,
			'template_id' => $data['template_id'],
			'miniprogram' => $miniprogram,
			'appid' => 'wxee43c66f451d0b81',
			'pagepath' => $data['page'],

			'data' => $data['data'],
		];
		$result = $this->WxTplMsg->sendTemplateMessage($params);
		//print_r($result);exit;
		// 请求微信api：发送模板消息
		// if ($status = $this->WxTplMsg->sendTemplateMessage($params)) {
		// 	$this->recordState("发送成功！");
		// }

		return true;
	}

}